
using System;
using System.Collections.Generic;
using UnityEngine;


namespace ActionSystem.Core
{
    /// <summary>
    /// StateConfig
    /// </summary>
    [System.Serializable]
    public class StateConfig
    {
        //状态基本信息
        public string stateName = "State";
        public List<string> animNames;//有些状态可能有多个动画，这些动画除了一些局部的不同，基本都一样。
        public int dafualtAnimIndex = 0;
        public Single fadeTime = 1 / (Single)20;
        public bool enableLoop = false;
        public string nextStateName = "";
        public int nextAnimIndex = -1;
        //状态具备的判断范围

        public List<FrameConfig> frames = new List<FrameConfig>();

        //状态具备的action
        [SerializeReference]
        public List<object> actions = new List<object>();

        public override string ToString() => stateName;

        public string defaultAnimaName => GetAnimName(dafualtAnimIndex);

        public string GetAnimName(int index)
        {
            return animNames?.Count > index ? animNames[index] : string.Empty;
        }
        //当前帧的判定范围
        public FrameConfig GetBodyRangesFrame(int frameIndex)
        {
            if (frames.Count == 0 || frameIndex < 0)
            {
                return null;
            }

            frameIndex %= frames.Count;
            FrameConfig config = frames[frameIndex];

            while (config.stayBodyRange)
            {
                --frameIndex;
                if (frameIndex < 0)
                {
                    return null;
                }
                config = frames[frameIndex];
            }

            return config;
        }

        public List<RangeConfig> GetBodyRanges(int frameIndex)
        {
            return GetBodyRangesFrame(frameIndex)?.bodyRanges;
        }

        public FrameConfig GetAttackRangesFrame(int frameIndex)
        {
            if (frames.Count == 0 || frameIndex < 0)
            {
                return null;
            }

            frameIndex %= frames.Count;
            FrameConfig config = frames[frameIndex];

            while (config.stayAttackRange)
            {
                --frameIndex;
                if (frameIndex < 0)
                {
                    return null;
                }
                config = frames[frameIndex];
            }

            return config;
        }

        public List<RangeConfig> GetAttackRanges(int frameIndex)
        {
            return GetAttackRangesFrame(frameIndex)?.attackRanges;
        }
    }
}